#pragma once
#include "afxwin.h"
#include "ItemInfo.h"
//#include "ItemInfo.h"
#include <vector>

#define OUT_FRAME_WIDTH						80
#define OUT_FRAME_HEIGHT					102
#define DEFAUTL_MARGIN						4
#define NAME_FRAME_HEIGHT					30
#define DEFAULT_PER_ROW_ITEM_COUNT			4
#define DEFAULT_EXTEND_MOUNT				3

#ifndef ITEM_INFO
#define ITEM_INFO 1
#endif

class CItemInfoEx;

class CTablet :
	public CWnd
{
	DECLARE_DYNAMIC(CTablet)
public:
	CTablet(int ncount)
		:m_itemCount(ncount)
		,m_pRowItemCount(DEFAULT_PER_ROW_ITEM_COUNT)
		,m_bScroll(FALSE)
		,m_InnerBound(OUT_FRAME_WIDTH-2*DEFAUTL_MARGIN, OUT_FRAME_HEIGHT-2*DEFAUTL_MARGIN-NAME_FRAME_HEIGHT)
		,m_OutterBound(OUT_FRAME_WIDTH,OUT_FRAME_HEIGHT)
		,m_MemDC(NULL)
		,m_pBitmap(NULL)
		,m_DrawMargin(0)
		,m_pActualHeight(0)
		,m_pRow(0)
		,m_pWinHeight(0)
		,m_bAdded(false)
	{
		
	}
	virtual ~CTablet( );
	virtual BOOL	Create( const RECT& rect, CWnd* pParentWnd, UINT nID, DWORD dwstyle=WS_CHILD|WS_VISIBLE);
	void			setProperty();
	void			InitFrame();
	void			AddOneItem(/*void* pData*/);
	void			AddOneItem(TCHAR *szExname, ...);
	void			DeleteOneItem( DWORD pData );
	void			SetVscrollInfo(int curH, int actH);
	

	
	
	

private:
	void				DrawRectangle(CDC* pDC,const CRect& rect,COLORREF clr,int nPensize =1);
	void				DrawszName(CDC* pDC,const CRect& rect,COLORREF clr,int nPenSize,TCHAR* szName);
	void				DrawItem(CRect& irect,TCHAR* szName);
	void				ExtendBitmap(/*int nwidth, */int ngrow=0);
	void				UpdateScrollbar( int nbottom );
	void				MptInRect(CPoint curPoint);
	inline void		UptoWindow(CDC* dc)
	{
		if(m_MemDC)
		{
			CRect rect;
			GetClientRect( &rect );

			if( m_bScroll )
			{
				SCROLLINFO sCrollInfo;
				GetScrollInfo( SB_VERT, &sCrollInfo );
				dc->BitBlt( 0, 0, rect.Width(), m_pActualHeight, m_MemDC, 0, sCrollInfo.nPos,  SRCCOPY );
			}else {
				dc->BitBlt( 0, 0, rect.Width(), m_pActualHeight, m_MemDC, 0, 0, SRCCOPY );
			}
		}
	}
	inline int			UpdateCount(BOOL bStatus=FALSE)
	{
		if(bStatus)
		{
			CItemInfoEx* pinfo = new CItemInfoEx();
			pinfo->m_pData = 0;
			m_pSData.push_back(pinfo);
		}
		return (bStatus) ? ++m_itemCount : --m_itemCount;
	}
	inline void			RedrawItems(bool bf=true)
	{
		CRect prect;
		prect.CopyRect( &m_initRect );

		m_bAdded = bf;

		for(int i=0;i<m_itemCount;++i)
		{
			if(i%m_pRowItemCount == 0)
			{
				prect.CopyRect(&m_initRect);
				prect.OffsetRect(0, (OUT_FRAME_HEIGHT + DEFAUTL_MARGIN) * (i/m_pRowItemCount) );
			}
			if(!m_bAdded)
			{
				CItemInfoEx* pinfo = new CItemInfoEx();
				pinfo->m_pData = 0;
				m_pSData.push_back(pinfo);
			}
			DrawItem( prect, _T("test") );
			/*if(i%m_pRowItemCount && i!=m_itemCount-1)
				prect.OffsetRect(OUT_FRAME_WIDTH+DEFAUTL_MARGIN,0);*/
			if( i!= (m_itemCount-1) && (i+1)%m_pRowItemCount  )
				prect.OffsetRect(OUT_FRAME_WIDTH+DEFAUTL_MARGIN,0);
		}
		m_lastRect.CopyRect( &prect );
	}
	inline void		CalcRandM()
	{
		CRect rect;
		GetClientRect( &rect );

		bool bCaled = false;
		bool bAdded = false;

		while(m_pRowItemCount * OUT_FRAME_WIDTH + (m_pRowItemCount - 1) * DEFAUTL_MARGIN >= rect.Width() && !bCaled )
		{
			m_pRowItemCount--;
		}

		if(m_pRowItemCount != DEFAULT_PER_ROW_ITEM_COUNT) {bCaled = true; }

		while(m_pRowItemCount * OUT_FRAME_WIDTH + (m_pRowItemCount-1) * DEFAUTL_MARGIN < rect.Width() && !bCaled )
		{
			m_pRowItemCount++;
			bAdded = true;
		}
		if(bAdded)
			m_pRowItemCount--;

		//calc the margin
		m_DrawMargin = (rect.Width() - m_pRowItemCount * OUT_FRAME_WIDTH - (m_pRowItemCount-1) * DEFAUTL_MARGIN )/2;

		//calc the rowth
		m_pRow = m_itemCount / m_pRowItemCount;
		if(m_itemCount % m_pRowItemCount)
			m_pRow++;
		/*if(m_pRow * OUT_FRAME_HEIGHT > rect.Height() )
		{
			SetVscrollInfo()
		}*/

		m_initRect.left = rect.left + m_DrawMargin;
		m_initRect.top = rect.top;
		m_initRect.right = m_initRect.left + OUT_FRAME_WIDTH;
		m_initRect.bottom = m_initRect.top + OUT_FRAME_HEIGHT;
	}

	


private:
	int										m_itemCount;
	int										m_pRowItemCount;
	BOOL									m_bScroll;
	CSize									m_InnerBound;
	CSize									m_OutterBound;
	COLORREF								m_bgCol;
	CDC*									m_MemDC;
	CBitmap*								m_pBitmap;
	int										m_DrawMargin;
	CRect									m_initRect;
	CRect									m_lastRect;
	int										m_pActualHeight;
	int										m_pRow;
	int										m_pWinHeight;
	std::vector<CItemInfoEx*>					m_pSData;
	std::vector<CItemInfoEx*>::iterator			m_pIter;
	bool									m_bAdded;

	
	

public:
	DECLARE_MESSAGE_MAP()
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
	afx_msg void OnPaint();
	afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
};
